package juc.atomic;

import java.util.concurrent.atomic.AtomicBoolean;

public class JITTest {
    //建议用volatile修饰或者用AtomicBoolen
    //private volatile static boolean init = false;

    private static boolean init = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread() {
            @Override
            public void run() {
                //while (!init) {
                //}
                /**
                 * while语句，把非volatile修饰的Boolean变量作为条件时，
                 * (1)没有操作内容时，jrt在运行时，会优化成 while(true){}
                 * (2)有操作内容时，jrt在运行时，不会优化
                 */
                while (!init) {
                    System.out.println(".");
                }

            }
        }.start();

        //休眠保证之前的线程已启动
        Thread.sleep(1000);

        new Thread() {
            @Override
            public void run() {
                init = true;
                System.out.println("Set init to true.");
            }
        }.start();
    }
}
